package com.woniuxy.controller;


import com.woniuxy.RedisLockConfig;
import com.woniuxy.annotation.DayOrderRecord;
import com.woniuxy.client.OrderClientRoom;
import com.woniuxy.config.RedisConfig;
import com.woniuxy.domain.Orderr;
import com.woniuxy.domain.Ticket;
import com.woniuxy.domain.User;
import com.woniuxy.dto.JsonResultDto;
import com.woniuxy.param.SeatXYParam;
import com.woniuxy.service.OrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuyanzu
 * @since 2021-05-30
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderClientRoom orderClientRoom;
    @Autowired
    private RedisConfig redisConfig;
    @Autowired
    private OrderService orderService;
    @Autowired
    private RedisLockConfig redisLockConfig;

    @GetMapping("/buyOrder")
    public JsonResultDto buyOrder(SeatXYParam seatXYParam, User user){
        JsonResultDto<Ticket> jsonResultDto = orderClientRoom.selectValidTicketByXY(seatXYParam);
        if(jsonResultDto.getCode().equals("1100")){
            return jsonResultDto;
        }else{
            //下面为座位可安排的情况
            //座位情况可安排就直接获得集合中的ticket对象更改ticket其中的数据

            orderClientRoom.changeTicketOrder(jsonResultDto.getObj());
            Orderr orderr = new Orderr();
            BeanUtils.copyProperties(jsonResultDto.getObj(), orderr);
            BeanUtils.copyProperties(user, orderr);
            orderr.setoStatus("2");
            orderService.insertOrder(orderr);
            //接下来是支付宝支付
            HashMap<Integer, Orderr> map = new HashMap<>();
            map.put(orderr.getoId(), orderr);

            //支付成功  order表 oStatus改为1
            //支付失败 order表  oStatus改为0 超时
            //       ticket表 tStatus同时改为1 表示可以购买
            return new JsonResultDto("1200","订购成功请付款",jsonResultDto.getObj(),null);
        }

    }
    @PutMapping("/changeOrderSuccess")
    public JsonResultDto changeOrderSuccess(Ticket ticket,HashMap<Integer, Orderr> map){
        Orderr orderr = map.get(ticket.gettId());
        orderClientRoom.changeOrderSuccess(ticket);
        orderr.setoStatus("1");
        orderService.updateById(orderr);
        return new JsonResultDto("1201","下单成功",null,null);
    }
    @PutMapping("/changeOrderFailed")
    public JsonResultDto changeOrderFailed(Ticket ticket,HashMap<Integer, Orderr> map){
        Orderr orderr = map.get(ticket.gettId());
        orderClientRoom.changeOrderFailed(ticket);
        orderr.setoStatus("0");
        orderService.updateById(orderr);
        return new JsonResultDto("0000","下单失败",null,null);
    }
    @PostMapping("/addListOrderTest")
    public JsonResultDto addListOrderTest(List<SeatXYParam> seatXYParams){



        for(SeatXYParam param:seatXYParams){
            try{
                if(redisLockConfig.lock(String.valueOf(param.getX()))){
                Orderr orderr = new Orderr();
                BeanUtils.copyProperties(param, orderr);
                orderService.insertOrder(orderr);

            }
            }finally {
                redisLockConfig.unlock(String.valueOf(param.getX()));
            }


        }
        return  null;
    }
    @DayOrderRecord
    @GetMapping("/addOrderTest")
    public void addOrderTest(){
        System.out.println("执行add");
    }
    //定时器决定电影状态m_status
//    @Scheduled(fixedDelay = 300000)
//    public void changeOrderByTime(){
//        if(orderService.list()==null)
//            return;
//        }else{
//            for(Order order:orderService.list()){
//                if((new Date().getTime()-order.getoStarttime().getTime())<0){
//                    order.setoStatus("0");
//                    orderService.updateById(order);
//                    Ticket ticket = orderClientRoom.selectTicketByCode(order.getTicketCode());
//                    ticket.settStatus("1");
//                    orderClientRoom.changeTicketOrder(ticket);
//                }
//            }
//        }
//    }
    @DayOrderRecord
    @GetMapping("/order")
    public void order(Orderr orderr){
        orderService.MovieRank(orderr);
    }
    @GetMapping("/getMovieRank")
    public Set<Object> getMovieRank(){
        return orderService.getMovieRank();
    }


    @GetMapping("/showDayRecord")
    public Integer showDayRecord(){
        return orderService.showDayRecord();
    }
    @GetMapping("/showMonthRecord")
    public Integer showMonthRecord(String month) throws Exception {
        return orderService.showMonthRecord(month);
    }
    @PostMapping("/orderRocket")
    public void orderRocket(Orderr orderr){
        orderService.orderRocket(orderr);
    }
//        @Scheduled(cron = "0/10 * * * * ?")
    public void record(){
        if(null==redisConfig.opsForValue().get("DayOrderRecord")){
            return;
        }
        int dayValue = Integer.parseInt((String) redisConfig.opsForValue().get("DayOrderRecord"));
        String time = new SimpleDateFormat("M-d").format(new Date());
        String month = time.split("-")[0];
        //如果月份表为空直接将统计订单数量放入
        if(null==redisConfig.opsForValue().get("DayOrderRecord:"+month)){
            redisConfig.opsForValue().set("MonthOrderRecord:"+month,String.valueOf(dayValue));
            //存入后销毁当日
            redisConfig.delete("DayOrderRecord");
        }else{
            Integer oldDayValue = Integer.parseInt((String) redisConfig.opsForValue().get("MonthOrderRecord:" + month));
            redisConfig.opsForValue().set("MonthOrderRecord:"+month,String.valueOf(oldDayValue+dayValue));
            //存入后销毁当日
            redisConfig.delete("DayOrderRecord");
        }

        System.out.println("record");
    }
}

